#!/usr/bin/env bash

start() {
	status
	if [ ! $? -eq 0 ]; then
		echo "process is already running.."
		return 1
	fi

	# 浙江旧版工商
	# nohup python old_zhejiang_crawl.py --thread_num 15 --store_tube gsxt_store > /dev/null 2>&1 &

    # 全国离线列表页抓取
    nohup python gsxt_list_crawl.py --process_num 1 --thread_num 10 --input_tube gsxt_list_download  --output_tube gsxt_detail_download --store_tube gsxt_store > /dev/null 2>&1 &

    # 浙江列表页
    nohup python gsxt_list_crawl.py --process_num 1 --thread_num 10 --input_tube zhejiang_list_download  --output_tube gsxt_detail_download --store_tube gsxt_store > /dev/null 2>&1 &

    # 测试用队列
    nohup python gsxt_list_crawl.py --process_num 1 --thread_num 1 --input_tube test_list_download  --output_tube gsxt_detail_download --store_tube gsxt_store > /dev/null 2>&1 &

    # 详情页抓取
    nohup python gsxt_detail_crawl.py --process_num 4 --thread_num 10 --input_tube gsxt_detail_download --store_tube gsxt_store > /dev/null 2>&1 &

    # 年报抓取，通过离线调度，不走在线，不依赖详情页前置信息，防止阻塞详情页抓取
    # nohup python gsxt_annual_crawl.py --process_num 4 --thread_num 6 --input_tube gsxt_annual_download --store_tube gsxt_store > /dev/null 2>&1 &

    # 详情页存储模块
    nohup python gsxt_detail_saver.py --input_tube gsxt_store --output_tube gsxt_detail_parse > /dev/null 2>&1 &

    # 工商调度脚本
    nohup python gsxt_master_scheduler.py > /dev/null 2>&1 &

    echo "process start success..."
}

stop() {

     # 关闭调度器
    ps -ef | grep python | grep -v grep | grep gsxt_master_scheduler | awk '{print $2}' | xargs kill -9

    # 关闭年报抓取
    # ps -ef | grep python | grep -v grep | grep gsxt_annual_crawl | awk '{print $2}' | xargs kill -9

    # 关闭详情页
    ps -ef | grep python | grep -v grep | grep gsxt_detail_crawl | awk '{print $2}' | xargs kill -9

	# 关闭列表页抓取
	ps -ef | grep python | grep -v grep | grep gsxt_list_crawl | awk '{print $2}' | xargs kill -9

    # 关闭旧版浙江工商列表页抓取
	# ps -ef | grep python | grep -v grep | grep old_zhejiang_crawl | awk '{print $2}' | xargs kill -9

    # 关闭存储模块
	ps -ef | grep python | grep -v grep | grep gsxt_detail_saver | awk '{print $2}' | xargs kill

    sleep 2

	status
	[ $? -eq 0 ] && echo "process stop success..." && return 1

	echo "process stop fail..."
	return 0
}

restart() {
    stop
    sleep 1
    start
}

status() {

    pid=`ps -ef | grep python | grep -v grep | grep gsxt_master_scheduler | awk '{print $2}'`
    if [ -z "${pid}" ]; then
        return 0
    fi
    echo "gsxt_master_scheduler ${pid}"

    pid=`ps -ef | grep python | grep -v grep | grep gsxt_list_crawl | awk '{print $2}'`
    if [ -z "${pid}" ]; then
        return 0
    fi
    echo "gsxt_list_crawl ${pid}"

#    pid=`ps -ef | grep python | grep -v grep | grep old_zhejiang_crawl | awk '{print $2}'`
#    if [ -z "${pid}" ]; then
#        return 0
#    fi
#    echo "old_zhejiang_crawl ${pid}"

    pid=`ps -ef | grep python | grep -v grep | grep gsxt_detail_crawl | awk '{print $2}'`
    if [ -z "${pid}" ]; then
        return 0
    fi
    echo "gsxt_detail_crawl ${pid}"

#    pid=`ps -ef | grep python | grep -v grep | grep gsxt_annual_crawl | awk '{print $2}'`
#    if [ -z "${pid}" ]; then
#        return 0
#    fi
#    echo "gsxt_annual_crawl ${pid}"

    pid=`ps -ef | grep python | grep -v grep | grep gsxt_detail_saver | awk '{print $2}'`
    if [ -z "${pid}" ]; then
        return 0
    fi
    echo "gsxt_store ${pid}"

    return 1
}

case "$1" in
	start|stop|restart|status)
  		$1
		;;
	*)
		echo $"Usage: $0 {start|stop|status|restart}"
		exit 1
esac
